package com.example.weddingapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.lifecycle.AndroidViewModel;


import com.example.weddingapp.R;
import com.example.weddingapp.base.BaseActivity;
import com.example.weddingapp.databinding.ActivityLoginBinding;
import com.example.weddingapp.utils.IdentifyingCode;

import cn.leancloud.AVUser;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class LoginActivity extends BaseActivity<AndroidViewModel, ActivityLoginBinding> implements View.OnClickListener {

    private String realCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setTitle("登录");
        setNoBackIcon();
        showContentView();

    }

    @Override
    protected void initView() {
        super.initView();
        bindingView.btnLogin.setOnClickListener(this);
        bindingView.tvRegister.setOnClickListener(this);
        bindingView.ivCode.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        //设置图片验证码
        bindingView.ivCode.setImageBitmap(IdentifyingCode.getInstance().createBitmap());
        //获取图片上的验证码
        realCode = IdentifyingCode.getInstance().getCode().toLowerCase();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_register:
                startActivity(new Intent(this, RegisterActivity.class));
                break;
            case R.id.btn_login:
                login();
                break;
            case R.id.iv_code:
                //设置图片验证码
                bindingView.ivCode.setImageBitmap(IdentifyingCode.getInstance().createBitmap());
                //获取图片上的验证码
                realCode = IdentifyingCode.getInstance().getCode().toLowerCase();
                break;
        }

    }

    /**
     * 登录
     */
    private void login() {
        String username = bindingView.etUsername.getText().toString();
        String password = bindingView.etPassword.getText().toString();
        String code = bindingView.etCode.getText().toString();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!realCode.toLowerCase().equals(code.toLowerCase())) {
            Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show();
            return;
        }

        AVUser.logIn(username, password).subscribe(new Observer<AVUser>() {
            public void onSubscribe(Disposable disposable) {
                hud.show();
            }

            public void onNext(AVUser user) {
                hud.dismiss();
                // 登录成功
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                finish();
            }

            public void onError(Throwable throwable) {
                hud.dismiss();
                // 登录失败（可能是密码错误）
                Toast.makeText(LoginActivity.this, "登录失败（" + throwable.getMessage() + "）", Toast.LENGTH_SHORT).show();
            }

            public void onComplete() {
            }
        });
    }
}
